package edu.uoc.tds.pec4.client;


import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
import java.sql.SQLException;
import javax.swing.JButton;
import edu.uoc.tds.pec4.beans.Usuario;
import edu.uoc.tds.pec4.client.gestion.ControladorConexion;
import edu.uoc.tds.pec4.i18n.TDSLanguageUtils;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Toolkit;
import java.awt.Color;




public class PanelConexionUI extends JFrame {

	/**
	 * @author asosal
	 *
	 */
	private static final long serialVersionUID = -8980796797653784251L;
	/**
	 * 
	 */
	private JPasswordField txtPassword;
	private JPanel panelPrincipal;
	private JTextField txtUsuario;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PanelConexionUI frame = new PanelConexionUI();
					frame.setVisible(true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("error.general"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PanelConexionUI() {
		
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		TDSLanguageUtils.setDefaultLanguage("i18n/ES_messages");
		setTitle(TDSLanguageUtils.getMessage("lbl.Titulo"));
		setIconImage (Toolkit.getDefaultToolkit().getImage(PanelConexionUI.class.getResource("LogoTurva.gif")));
		initComponents();
        SetPanelInicial();
		
	}
	 private void initComponents() 
	 {
		    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 550, 300);
			panelPrincipal = new JPanel();
			panelPrincipal.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(panelPrincipal);
			panelPrincipal.setLayout(null);
			
			JButton btnLogin = new JButton("Login");
			btnLogin.setText(TDSLanguageUtils.getMessage("btInicioSesion.button"));
			btnLogin.setBounds(310, 176, 136, 23);
			panelPrincipal.add(btnLogin);
			
			JLabel lblUsuario = new JLabel("New label");
			lblUsuario.setText(TDSLanguageUtils.getMessage("lblUsuario.label"));
			lblUsuario.setBounds(240, 74, 91, 14);
			panelPrincipal.add(lblUsuario);
			
			JLabel lblPassword = new JLabel("New label");
			lblPassword.setText(TDSLanguageUtils.getMessage("lblPassword.label"));
			lblPassword.setBounds(240, 115, 91, 14);
			panelPrincipal.add(lblPassword);
			
			txtUsuario = new JTextField();
			txtUsuario.setBounds(341, 71, 157, 20);
			panelPrincipal.add(txtUsuario);
			txtUsuario.setColumns(10);
			
			txtPassword = new JPasswordField();
			txtPassword.setColumns(10);
			txtPassword.setBounds(341, 112, 157, 20);
			panelPrincipal.add(txtPassword);
			
			JLabel labelLogo = new JLabel("");
			labelLogo.setBounds(52, 45, 148, 159);
			labelLogo.setIcon(new ImageIcon(PanelMenusUI.class.getResource("LogoInicio.png")));
			panelPrincipal.add(labelLogo);
			
			JLabel lblgrupo = new JLabel(TDSLanguageUtils.getMessage("lbl.Titulo.Grupo"));
			lblgrupo.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
			lblgrupo.setForeground(new Color(0, 153, 51));
			lblgrupo.setBounds(52, 219, 224, 14);
			panelPrincipal.add(lblgrupo);
			
			JLabel lblBienvenida = new JLabel(TDSLanguageUtils.getMessage("lbl.Titulo.Bienvenida"));
			lblBienvenida.setForeground(new Color(0, 153, 51));
			lblBienvenida.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
			lblBienvenida.setBounds(52, 20, 190, 14);
			panelPrincipal.add(lblBienvenida);
			
			
		    btnLogin.addMouseListener(new java.awt.event.MouseAdapter() 
		        {
		            public void mouseReleased(java.awt.event.MouseEvent evt)
		            {
		                 btnLoginMouseReleased(evt);
		            }

					private void btnLoginMouseReleased(MouseEvent evt) 
					{
						
						ConectarServidor();
											
					}
		 });
	 }
	 private void SetPanelInicial() 
	 {
	        panelPrincipal.setVisible(true);
	       
	 }
	 private void CloseFrameInicial() 
	 {
	       super.dispose(); 
		 
	 }
	 private boolean ConectarServidor() throws NumberFormatException {
	        try {
	        	String pass;
	        	 try{
	        		  pass = new String(txtPassword.getPassword());	       		
	        		  int rol = ControladorConexion.conexionUsuario(txtUsuario.getText(), pass);
	        		  switch(rol)
	        		  {
	        		  case 0: 	  JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Error.usuario.noEncontrado"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);	
	        		  			  break;
	        		  case 1: 	  JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Error.password.Incorrecta"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);	
	        		  			  break;
	        		  case 2:    JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Informacion.Bienvenida"), TDSLanguageUtils.getMessage("informacion"), 1);
	        		  				Usuario usuario = ControladorConexion.getUsuarioPorLogin(txtUsuario.getText());
	        		                PanelMenusUI frameMenu = new PanelMenusUI(usuario);	        		  				       		  				
	        		  				frameMenu.setVisible(true);	 
	        		  				CloseFrameInicial();
	        		  }
	         	 }
	        	 catch(NumberFormatException ex){
	        		 JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Error.problema.conversion"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
	    	 		}   
		        
	            return true;
	           

			} catch (RemoteException e) {
				JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("cliente.error.conectar"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("mensaje.remoto.conexionFallida"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);					
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("error.general"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
			}
	        return false;
	        
	    }
}
	
	
		
		

